from pynput.keyboard import Key, Controller  # 导入按键控制
import time  # 导入时间包，调用sleep函数让琴键有时间间隔
import threading  # 导入线程包


# 演奏函数
def play_piano(music):
    for n in music:
        if n.isupper():
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press(n.lower())
            time.sleep(0.499)
            keyboard.release(n.lower())
            keyboard.release(Key.shift)
        elif n in "!@$%^*(":
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press("1245689"["!@$%^*(".index(n)])
            time.sleep(0.499)
            keyboard.release("1245689"["!@$%^*(".index(n)])
            keyboard.release(Key.shift)
        elif n != " " and n != "-":
            keyboard.press(n)
            time.sleep(0.5)
            keyboard.release(n)
        elif n == "-":
            time.sleep(1)
        else:
            time.sleep(0.5)


if __name__ == '__main__':
    # 准备演奏
    # 控制键盘
    keyboard = Controller()
    # 切换到vue键盘钢琴(auto piano)网页
    keyboard.press(Key.alt)
    time.sleep(1)
    keyboard.press(Key.tab)
    keyboard.release(Key.tab)
    keyboard.release(Key.alt)
    time.sleep(2)
    # 右手
    right = "!@QWETY"
    # # 左手
    # left = "-- qtu qry  0ry 60t  9et 5qr  8wty8 $  "
    # # 右手拇指
    # rightThumb = "--p- a-  d- s-  s- p d  o- u Qf "
    # # 右手食指
    # rightIndexFinger = "--f- d-  ---- f- s-  f- -s  "
    # # 左手手拇指
    # leftThumb = "-- -  -  ---- ---- --u - "
    # # 左手食指
    # leftIndexFinger = "-- -  -  ---- ---- ---- "
    # 运行线程
    rt = threading.Thread(target=play_piano, args=(right,))  # 主旋律线程
    # lt = threading.Thread(target=play_piano, args=(left,))  # 和弦线程
    # rtt = threading.Thread(target=play_piano, args=(rightThumb,))  # 右手拇指线程
    # rift = threading.Thread(target=play_piano, args=(rightIndexFinger,))  # 右手食指线程
    # ltt = threading.Thread(target=play_piano, args=(leftThumb,))  # 左手拇指线程
    # lift = threading.Thread(target=play_piano, args=(leftIndexFinger,))  # 右手食指线程
    rt.start()
    # lt.start()
    # rtt.start()
    # rift.start()
    # ltt.start()
    # lift.start()
